package com.gome.wechat.program.service.queryservice.impl;

import com.gome.wechat.program.dto.UserDto;
import com.gome.wechat.program.entity.User;
import com.gome.wechat.program.entity.UserHobbyTag;
import com.gome.wechat.program.service.UserHobbyTagService;
import com.gome.wechat.program.service.UserService;
import com.gome.wechat.program.service.queryservice.UserQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liwei113 on 2017/11/2 0002.
 */
@Service
public class UserQueryServiceImpl implements UserQueryService {

    @Autowired
    UserService userService;

    @Autowired
    UserHobbyTagService userHobbyTagService;


    @Override
    public UserDto findByOpenId(String openId) {
        User user =  userService.findByOpenId(openId);
        if(user == null){
            return null;
        }
        UserDto result = UserDto.create(user.getId(),user.getHeadImage(),user.getName(),user.getOpenId(),user.getSex(),
                user.getAge(),user.getIntroduction());
        List<UserHobbyTag> userHobbyTags = userHobbyTagService.findByUserId(user.getId());
        List<Long>  tagIds = new ArrayList<>();
        for(UserHobbyTag temp:userHobbyTags){
            tagIds.add(temp.getTagId());
        }
        result.setHobbyIds(tagIds);
        return result;
    }
}
